<template>
  <el-dialog
    title="添加商品类别"
    :visible="goodsTypeVisible"
    @update:visible="v=>$emit('update:goodsTypeVisible',false)"
  >
    <el-form>
      <el-form-item :label="'商品类别名称:'+$route.name">
        <el-input v-model="goodsTypeForm.goodsTypeName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="是否推荐:">
        <el-radio v-model="goodsTypeForm.radio" label="1">推荐</el-radio>
        <el-radio v-model="goodsTypeForm.radio" label="2">不推荐</el-radio>
      </el-form-item>
      <el-form-item label="所在店铺类型:">
        <el-select
          @change="changeShopType"
          v-model="goodsTypeForm.shopTypeId"
          width="80px"
          placeholder="请选择"
        >
          <el-option
            v-for="item in $store.state.shop.allshopTypeList"
            :key="item._id"
            :label="item.shopTypeName"
            :value="item._id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label=" 所 在 店 铺 : ">
        <el-select v-model="goodsTypeForm.shopId" placeholder="请选择">
          <el-option
            v-for="item in $store.state.shop.shopListByTypeId"
            :key="item._id"
            :label="item.shopName"
            :value="item._id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('update:goodsTypeVisible',false)">取 消</el-button>
      <el-button type="primary" @click="submitGoodsType">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "add-shop",
  props: {
    goodsTypeVisible: {
      type: Boolean,
      default: false
    },
    shopTypeId: {
      type: String,
      default: ""
    },
    shopId: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      goodsTypeForm: {
        goodsTypeName: "",
        shopTypeId: "",
        shopId: "",
        radio: "1"
      }
    };
  },
  mounted() {
    console.log(this.$route.name);
    this.$store.dispatch("allgetShopTypeList");
    if (this.shopTypeId) {
      this.goodsTypeForm.shopTypeId = this.shopTypeId;
      this.changeShopType(this.shopTypeId);
      if (this.shopId) {
        this.goodsTypeForm.shopId = this.shopId;
      }
    }
  },
  methods: {
    //选择店铺名的时候触发事件,获取店铺类型相对应所有店铺
    changeShopType(shopTypeId) {
      this.$store.dispatch("allgetshopListByTypeId", shopTypeId);
      this.goodsTypeForm.shopId = null;
    },
    //提交商品类型表单数据
    submitGoodsType() {
      this.$store.dispatch("submitGoodsType", this.goodsTypeForm).then(res => {
        if (res.ok === 1) {
          // console.log(res);
          this.$emit("update:goodsTypeVisible", false);
          // console.log(this.$route.name);
          if (this.$route.name !== "goodsTypeList") {
            this.$router.history.push("/goodsTypeList");
          }
          this.$store.dispatch("getGoodsTypeList");
        }
      });
    }
  }
};
</script>

